package com.jqbd.controller;

import com.jqbd.common.MyJSON;
import com.jqbd.common.R;
import com.jqbd.pojo.User;
import com.jqbd.service.impl.UserServiceImpl;
import com.jqbd.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * Created by Enzo Cotter on 2022/11/10.
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    @RequestMapping("/findAll")
    public MyJSON<User> queryAllVaccineVaccineController(@RequestParam Map<String, Object> map) {
        return userService.findAll(map);
    }
    //查询用户职责
    @RequestMapping("/selectnickname")
    public MyJSON<String> selectnickname(){
        return userService.selectnickname();
    }
    @RequestMapping("/deleteByPrimaryKey")
    public R deleteByPrimaryKey(Integer id){
        int i = userService.deleteByPrimaryKey(id);
        return i>0?R.ok():R.error();
    }
    @RequestMapping("/insert")
    public R insert(User user){
        int insert = userService.insertSelective(user);
        return insert>0 ?R.ok():R.error();
    }

//    int insertSelective(User record);
    //查询个人信息
    @RequestMapping("/selectByPrimaryKey")
    public R selectByPrimaryKey(Integer id){
        User user = userService.selectByPrimaryKey(id);

        if (user!=null){
            return R.ok().data("user",user);
        }else{
            return R.error();
        }
    }
    @RequestMapping("/updateByPrimaryKey")
    public R updateByPrimaryKey(User user){
        int i = userService.updateByPrimaryKeySelective(user);
        return i>0?R.ok():R.error();
    }
    @RequestMapping("/load")
    public R load(MultipartFile file) throws IOException {
        String picUrl = OssUtil.uploadFile(file.getInputStream(), file.getOriginalFilename());
        return picUrl != null ? R.ok().data("picUrl",picUrl) : R.error().data("url","图片找不到了,请重新上传");
    }

}
